// by Xeno
//#define __DEBUG__
#define THIS_FILE "fn_artymapclick.sqf"
#include "x_setup.sqf"

#include "x_uiinc.sqf"

private ["_ctrl", "_idxsel", "_mag", "_wpos", "_warty", "_inrange", "_eta", "_map", "_posx", "_posy"];

__TRACE_1("","_this");

PARAMS_4(_map,_but,_posx,_posy);

if (ctrlShown _map) then {
	__TRACE("Map shown");
	_pp = _map ctrlMapScreenToWorld [_posx, _posy];
	GVAR(ARTI_HELPER) setPos _pp;
	player reveal GVAR(ARTI_HELPER);
	GVAR(ARTI_MARKER_HELPER) setMarkerPos _pp;
};

_ctrl = __uiGetVar(D_ARTI_DISP) displayCtrl 888;
_idxsel = lbCurSel _ctrl;
__TRACE_1("","_idxsel");
if (_idxsel == -1) exitWith {};
_mag = _ctrl lbData _idxsel;
__TRACE_1("","_mag");

//_wpos = position GVAR(ARTI_HELPER);
_wpos = markerPos GVAR(ARTI_MARKER_HELPER);
_wpos set [2,0];
__TRACE_1("","_wpos");

_warty = GVAR(ao_arty_vecs) select 0;
__TRACE_1("","_warty");

_inrange = _wpos inRangeOfArtillery [[_warty], _mag];

_eta = if (_inrange) then {_warty getArtilleryETA [_wpos, _mag]} else {0};

_ctrl = __uiGetVar(D_ARTI_DISP) displayCtrl 900;
_ctrl ctrlSetText format [localize "STR_DOM_MISSIONSTRING_1458", _eta];

_ctrl = __uiGetVar(D_ARTI_DISP) displayCtrl 890;
if (_inrange) then {
	_ctrl ctrlSetText localize "STR_DOM_MISSIONSTRING_1244";
	_ctrl ctrlEnable true;
} else {
	_ctrl ctrlSetText localize "STR_DOM_MISSIONSTRING_1244a";
	_ctrl ctrlEnable false;
};